<?php

namespace pay\modules\backend\models;

use pay\libs\Tools;
use Yii;

/**
 * This is the model class for table "pay_configuration".
 *
 * @property int $id
 * @property int $aid 应用id
 * @property int $cid 渠道id
 * @property string $channel_mer_id 渠道商户id
 * @property string $status 1-启用，2-禁用
 * @property string|null $remake 备注
 * @property string|null $configuration 支付配置
 */
class Configuration extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_configuration';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['cid', 'channel_mer_id','app_id','aid'], 'required'],
            [['cid','app_id','aid'], 'integer'],
            [['status','create_time'], 'string'],
            [['channel_mer_id'], 'string', 'max' => 32],
            [['remake'], 'string', 'max' => 255],
            [['configuration'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'app_id' => 'App Id',
            'aid' => 'Aid',
            'cid' => 'Cid',
            'channel_mer_id' => 'Channel Mer ID',
            'status' => 'Status',
            'remake' => 'Remake',
            'configuration' => 'Configuration',
            'create_time' => '创建时间'
        ];
    }

    /*** 新增支付服务
     * @param $data
     * @return bool|void
     */
    public function addConfig($data)
    {
        if (isset($data['app_id']) and !empty($data['app_id'])){
            $params['Configuration']['app_id'] = $data['app_id'];
        }else{
            return $this->addError('app_id','Unauthorized');
        }
        if (isset($data['aid']) and !empty($data['aid'])){
            $params['Configuration']['aid'] = $data['aid'];
        }else{
            return $this->addError('aid','Unauthorized');
        }
        if (isset($data['cid']) and !empty($data['cid'])){
            $params['Configuration']['cid'] = $data['cid'];
        }else{
            return $this->addError('cid','Unauthorized');
        }
        if (isset($data['channel_mer_id']) and !empty($data['channel_mer_id'])){
            $params['Configuration']['channel_mer_id'] = $data['channel_mer_id'];
        }else{
            return $this->addError('channel_mer_id','Unauthorized');
        }

        if (isset($data['status']) and !empty($data['status'])){
            $params['Configuration']['status'] = $data['status'];
        }else{
            return $this->addError('status','Unauthorized');
        }
        if (isset($data['remake']) and !empty($data['remake'])){
            $params['Configuration']['remake'] = $data['remake'];
        }else{
            $params['Configuration']['remake'] = '';
        }
        if (isset($data['configuration']) and !empty($data['configuration'])){
            $params['Configuration']['configuration'] = $data['configuration'];
        }else{
            return $this->addError('configuration','Unauthorized');
        }
        if (isset($data['id']) and !empty($data['id'])){
            $model = self::find()->where('id = :id',[':id' => $data['id']])->one();
        }else{
            $info = self::find()->where(['app_id' => $params['Configuration']['app_id'],'aid' => $params['Configuration']['aid'],'cid' => $params['Configuration']['cid']])->asArray()->one();
            if (!empty($info)){
                return false;
            }



        }
        $params['Configuration']['create_time'] = date("Y-m-d H:i:s");
        if (!is_null($model)){
            $res = $this->modConfig($model,$params);
            return $res;
        }
        if ($this->load($params) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;

    }


    /*** 修改支付服务
     * @param $model
     * @param $params
     * @return bool
     */
    public function modConfig($model,$params)
    {
        if ($this->load($params) and $this->validate()) {
            $model->channel_mer_id = $params['Configuration']['channel_mer_id'];
            $model->status = $params['Configuration']['status'];
            $model->remake = $params['Configuration']['remake'];
            $model->configuration = $params['Configuration']['configuration'];
            if ($model->save(0)){
                return true;
            }
        }
        return false;
    }





    /*** 获取已开通支付服务的cid
     * @param $aid
     * @return array
     */
    public function ConfigCid($aid)
    {
        $info = self::find()->where('aid = :id',[':id' => $aid])->select('cid')->asArray()->all();
        if (!empty($info)){
            foreach ($info as $key => $val){
                $cid[] = $val['cid'];
            }

        }else{
            $cid = [];
        }
        return $cid;

    }



    /*** 支付配置管理支付渠道
     * @return \yii\db\ActiveQuery
     */
    public function getChannel()
    {
        $data = $this->hasOne(PaymentChannel::className(),['cid' => 'cid'])->select(['cid','channel_id','type','scenario']);
        return $data;
    }

    public function getApplicte()
    {
        $data = $this->hasOne(PaymentChannel::className(),['cid' => 'cid']);
        return $data;
    }
}
